﻿using Bot.Domain.Bot;
using Bot.Domain.Conversation;
using Bot.Domain.EndpointAPIResponseRecords;
using Bot.Domain.SessionChat;


using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bot.Domain.Report
{
    public class Last7DaysSummaryItem : BaseReportInfoItem
    {  

    }
    public class Last7DaysSummaryInfo : BaseReportInfo<Last7DaysSummaryItem>
    {
        public double AverageRatingScore { get; set; }
        public int BotOnlyChatCount { get; set; }

        public double BotOnlyChatsTime { get; set; }

        public int BotAnswerCount { get; set; }

        public int ChatsFromBotToAgentCount { get; set; }
        public double PercentageOfBotOnlyChats { get; set; }
      
        public double PercentageOfHighConfidenceAnswers { get; set; }
      

        [JsonIgnore]
        public double AverageCostPerChat { get; set; }

        public double HighConfidenceAnswerCount { get; set; }
        [JsonIgnore]
        public new EnumReportDataType DataType { get { return dataType; } }
          
        [JsonIgnore]
        public override IList<Last7DaysSummaryItem> DataList { get { return dataList; } }
    }
    public class Last7DaysSummaryEntityInfo
    {
        public IList<SessionChatDto> Last7DaysSummaryEntityList { get; set; }
        public IList<EndpointAPIResponseRecordsOutputDto> ResponseEntityList { get; set; }
        public List<ConversationDto> ConversationList { get; set; }
        //public BotEngineSiteMappingDto botEngineSiteMappingDto { get; set; }
    }

    public class Last7DaysSummaryWrap : BasicWrap<Last7DaysSummaryItem, Last7DaysSummaryInfo, Last7DaysSummaryEntityInfo>
    {
        
        public static Last7DaysSummaryInfo GetLast7DaysSummary(SearchCriteria search, Last7DaysSummaryEntityInfo entityInfo, bool ifEncode = true)
        {

            Last7DaysSummaryInfo result = new Last7DaysSummaryInfo();
            //UTCToLocal(ref entityInfo);

            int[] onlyBotStatus = new int[] { 0, 1, 2 };

            double agentOnlyCount = entityInfo.Last7DaysSummaryEntityList.Count(a => a.Status == 0 && a.ChatType == (int)EnumChatType.AgentOnly);
            double botOnlyChatsCount = entityInfo.Last7DaysSummaryEntityList.Count(a => onlyBotStatus.Contains(a.Status) && a.ChatType == (int)EnumChatType.ChatbotOnly);
            double chatsFromBotToAgentCount = entityInfo.Last7DaysSummaryEntityList.Count(a => a.Status == 0 && a.ChatType == (int)EnumChatType.ChatbotToAgent);

            double agentOnlyScoreCount = entityInfo.Last7DaysSummaryEntityList.Count(a => a.RatingGrade != -1 && a.Status == 0 && a.ChatType == (int)EnumChatType.AgentOnly);
            double botOnlyChatsScoreCount = entityInfo.Last7DaysSummaryEntityList.Count(a => a.RatingGrade != -1 && onlyBotStatus.Contains(a.Status) && a.ChatType == (int)EnumChatType.ChatbotOnly);
            double chatsFromBotToAgentScoreCount = entityInfo.Last7DaysSummaryEntityList.Count(a => a.RatingGrade != -1 && a.Status == 0 && a.ChatType == (int)EnumChatType.ChatbotToAgent);

            double agentOnlyScore = entityInfo.Last7DaysSummaryEntityList.Where(a => a.RatingGrade != -1 && a.Status == 0 && a.ChatType == (int)EnumChatType.AgentOnly).Sum(a => a.RatingGrade);
            double botOnlyChatsScore = entityInfo.Last7DaysSummaryEntityList.Where(a => a.RatingGrade != -1 && onlyBotStatus.Contains(a.Status) && a.ChatType == (int)EnumChatType.ChatbotOnly).Sum(a => a.RatingGrade);
            double chatsFromBotToAgentScore = entityInfo.Last7DaysSummaryEntityList.Where(a => a.RatingGrade != -1 && a.Status == 0 && a.ChatType == (int)EnumChatType.ChatbotToAgent).Sum(a => a.RatingGrade);

            double totalScore = agentOnlyScore + botOnlyChatsScore + chatsFromBotToAgentScore;
            double totalScoreChats = agentOnlyScoreCount + botOnlyChatsScoreCount + chatsFromBotToAgentScoreCount;
            double totalChats = agentOnlyCount+ botOnlyChatsCount+ chatsFromBotToAgentCount;

            double totalChatbotOnlyTime = entityInfo.Last7DaysSummaryEntityList.Where(a => onlyBotStatus.Contains(a.Status) && a.ChatType == (int)EnumChatType.ChatbotOnly).Sum(a => a.EndTime.Subtract(a.StartTime).TotalSeconds);

            double botResponsesCount = entityInfo.ResponseEntityList.Count();
            double highConfidenceResponsesCount= entityInfo.ResponseEntityList.Count(a => a.AnswerType == (int)EnumChatbotAnswerType.HighConfidenceAnswer);


            //conversation 2019.12.5
            if(search.SummaryChannel == "livechat")
            {

            }
            else
            {
                botOnlyChatsCount = entityInfo.ConversationList.Count(a=>a.AssignedBotId!=Guid.Empty&&a.AssignedAgentId==Guid.Empty&&a.AssignedDepartmentId==Guid.Empty);
                chatsFromBotToAgentCount = entityInfo.ConversationList.Count(a => a.AssignedBotId != Guid.Empty && (a.AssignedAgentId != Guid.Empty || a.AssignedDepartmentId != Guid.Empty));
                totalChats = entityInfo.ConversationList.Count();
            }


            //double botCosts = (double)entityInfo.botEngineSiteMappingDto.BotCosts;
            //double numberOfQuestionsPurchased = (double)entityInfo.botEngineSiteMappingDto.NumberOfQuestionsPurchased;
            //double price = botCosts / numberOfQuestionsPurchased;

            result.BotOnlyChatCount = (int)botOnlyChatsCount;
            result.PercentageOfBotOnlyChats = CalcAvg(botOnlyChatsCount* 100, totalChats);
            result.BotOnlyChatsTime = CalcAvg(totalChatbotOnlyTime, 60);
            result.ChatsFromBotToAgentCount = (int)chatsFromBotToAgentCount;
            result.BotAnswerCount = (int)botResponsesCount;
            result.HighConfidenceAnswerCount = highConfidenceResponsesCount;
            result.PercentageOfHighConfidenceAnswers = CalcAvg(highConfidenceResponsesCount* 100, botResponsesCount);
            result.AverageRatingScore = CalcAvg(botOnlyChatsScore, botOnlyChatsScoreCount);
            //result.AverageCostPerChat = CalcAvg(highConfidenceResponsesCount * price, botOnlyChatsCount+ chatsFromBotToAgentCount,3);
            result.dataType = EnumReportDataType.DataByTime;
            
            //watch.Stop();
            //System.Diagnostics.Debug.WriteLine("Calc Time: " + watch.Elapsed);

            return result;
        }

        #region export

        ////search the New Export CSV Data for Chat Source Info by criteria
        //public static NewExportCSVData GetExprotData(SearchCriteria search)
        //{
        //    Last7DaysSummaryourceInfo info = GetLast7DaysSummaryource(search, false);
        //    NewExportCSVData result = new NewExportCSVData(string.Empty, new List<NewCSVColumn>(), new DataTable());
        //    switch (search.dimensionType)
        //    {
        //        case EnumDimensionType.ByTime://by time
        //        case EnumDimensionType.ByDepartment://by department
        //            {
        //                result = GetExprotCSVData(search, info, info.FieldList);
        //                break;
        //            }                 
        //        case EnumDimensionType.RequestPage:
        //            {
        //                result = GetExprotCSVData(search, info, new List<string>() { info.FieldList[0]});
        //                break;
        //            }
        //    }
        //    return result;
        //}       

        #endregion

    }
}
